<?php

namespace App\Http\Controllers\Home;

use App\Model\Company;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
//公司管理
class CompanyController extends CommonController
{
    //
	protected $model;
	public function __construct()
	{
		parent::__construct();
		$this->model=new Company();
	}


	public function lists(Request $request){
		try{

			$where['name']=isset($request->name)?$request->name:null;
			$where['start_time']=isset($request->start_time)?$request->start_time:null;
			$where['end_time']=isset($request->end_time)?$request->end_time:null;
			$where['status']=isset($request->status)?$request->status:null;
			$data=$this->model->lists($where);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}

	public function add(Request $request){

		$this->validate($request,[
			'name'=>'required|unique:company|max:50',
			'organization_code'=>'required|unique:company|Numeric',
			'address'=>'required|string',
			'linkman'=>'required|String',
			'phone'=>'required|Numeric',
			'contract_code'=>'Numeric',
			'start_time'=>'required|date',
			'end_time'=>'required|date',
			'salesman'=>'required|string',
			'license'=>'string',
		]);

		DB::beginTransaction();
		try{
			$data=$request->only($this->model->getFillable());
			$result=$this->model->create($data);
//			添加角色
//			$this->addRole($result->id);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}
	public function edit(Request $request){
		$id=$request->id;
		try{
			$data=$this->model->where(['id'=>$id])->first();
			$data['statu']=$data['status']==0?'无效':'有效';
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}
	public function save(Request $request){
		$id=$request->id;
		$this->validate($request,[
			'name'=>'required|unique:company|max:50',
			'organization_code'=>'required|unique:company|Numeric',
			'address'=>'required|string',
			'linkman'=>'required|String',
			'phone'=>'required|Numeric',
			'contract_code'=>'Numeric',
			'start_time'=>'required|date',
			'end_time'=>'required|date',
			'salesman'=>'required|string',
			'license'=>'string',
		]);
		$data=$request->only($this->model->getFillable());
		try{
			$data=$this->model->where(['id'=>$id])->update($data);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($data);
		}else{
			return $this->failed($message);
		}
	}

	public function status(Request $request){
		$id=$request->id;
		$status=$request->status;
		try{
			$data=$this->model->where(['id'=>$id])->update(['status'=>$status]);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($data);
		}else{
			return $this->failed($message);
		}
	}


//	public function addRole($company_id){
//		return 1;
//	}
}
